<template>

  <!-- 列表 -->
  <ContentWrap v-loading="loading">
    <LayoutElementCanvas :layout-elements="list"/>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="布局图元标识" align="center" prop="id"/>
      <el-table-column label="所属布局图" align="center" prop="layoutId"/>
      <el-table-column label="关联设备" align="center" prop="deviceId"/>
      <el-table-column label="图元名称" align="center" prop="name"/>
      <el-table-column label="标题文字" align="center" prop="title"/>
      <el-table-column label="类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.LAYOUT_ELEMENT_TYPE" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="相对水平位置" align="center" prop="positionX"/>
      <el-table-column label="相对垂直位置" align="center" prop="positionY"/>
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['yz:layout-element:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['yz:layout-element:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

</template>

<script setup lang="ts">
import {DICT_TYPE} from '@/utils/dict'
import {dateFormatter} from '@/utils/formatTime'
import * as LayoutElementApi from '@/api/yz/layoutelement'
import LayoutElementCanvas from './ScreenDesignCanvas.vue'

const props = defineProps({
  layoutId: {
    type: Number,
    default: undefined
  }
})

const message = useMessage() // 消息弹窗
const {t} = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const queryParams = computed(() => ({
  pageNo: 1,
  pageSize: 100,
  layoutId: props.layoutId,
}))

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await LayoutElementApi.getLayoutElementPage(queryParams.value)
    list.value = data.list
  } finally {
    loading.value = false
  }
}
// layoutId变化时，刷新列表
watch(() => props.layoutId, getList)

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await LayoutElementApi.deleteLayoutElement(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {
  }
}
const addElement = (element) => {
  const layoutElement = {
    ...element,
    layoutId: props.layoutId,
  }
  list.value.push(layoutElement);
}

const saveElements = async () => {
  loading.value = true
  try {
    await LayoutElementApi.updateLayoutElementByLayoutId({
      layoutId: props.layoutId,
      list: list.value,
    })
    loading.value = false
    message.success(t('common.updateSuccess'))
  } catch (e) {
    loading.value = false
  }


}
defineExpose({
  addElement,
  saveElements
})
</script>
